package menu.lt.pers.menu.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {
    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql_user="CREATE Table "+Dbdata.userTable+" (" +
                Dbdata.userColumn[0]+" INTEGER primary key autoincrement," +
                Dbdata.userColumn[1]+" TEXT," +
                Dbdata.userColumn[2]+" INTEGER," +
                Dbdata.userColumn[3]+" TEXT," +
                Dbdata.userColumn[4]+" TEXT," +
                Dbdata.userColumn[5]+" TEXT)\n";

        String sql_class = "CREATE Table "+ Dbdata.classtable +" ("+
                Dbdata.classColumn[0]+" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                Dbdata.classColumn[1]+" TEXT," +
                Dbdata.classColumn[2]+" INTEGER NOT NULL);\n";

        String sql_collection = "CREATE TABLE "+ Dbdata.collectiontable +" ("+
                Dbdata.collectionColumn[0]+" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"+
                Dbdata.collectionColumn[1]+" INTEGER NOT NULL,"+
                Dbdata.collectionColumn[2]+" TEXT,"+
                Dbdata.collectionColumn[3]+" INTEGER,"+
                Dbdata.collectionColumn[4]+" TEXT,"+
                Dbdata.collectionColumn[5]+" TEXT,"+
                Dbdata.collectionColumn[6]+" TEXT,"+
                Dbdata.collectionColumn[7]+" TEXT,"+
                Dbdata.collectionColumn[8]+" TEXT,"+
                Dbdata.collectionColumn[9]+" TEXT,"+
                Dbdata.collectionColumn[10]+" TEXT,"+
                Dbdata.collectionColumn[11]+" INTEGER NOT NULL);";
        String sql_history = "CREATE TABLE "+ Dbdata.historytable +" ("+
                Dbdata.historyColumn[0]+" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"+
                Dbdata.historyColumn[1]+" INTEGER NOT NULL,"+
                Dbdata.historyColumn[2]+" TEXT,"+
                Dbdata.historyColumn[3]+" INTEGER,"+
                Dbdata.historyColumn[4]+" TEXT,"+
                Dbdata.historyColumn[5]+" TEXT,"+
                Dbdata.historyColumn[6]+" TEXT,"+
                Dbdata.historyColumn[7]+" TEXT,"+
                Dbdata.historyColumn[8]+" TEXT,"+
                Dbdata.historyColumn[9]+" TEXT,"+
                Dbdata.historyColumn[10]+" TEXT,"+
                Dbdata.historyColumn[11]+" INTEGER NOT NULL,"+
                Dbdata.historyColumn[12]+" );";

        Log.i("DBHelper.onCreate", "onCreate: "+sql_class);
        Log.i("DBHelper.onCreate", "onCreate: "+sql_collection);
        Log.i("DBHelper.onCreate", "onCreate: "+sql_history);
        db.execSQL(sql_user);
        db.execSQL(sql_class);
        db.execSQL(sql_collection);
        db.execSQL(sql_history);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql="delete Table if exists "+Dbdata.userTable;
        db.execSQL(sql);
        onCreate(db);
    }
}
